/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapsActivity extends MapActivity
{    

	private MapView mapView;
	private GeoPoint destination;
	private MapController mapController;

	private int minLat = (int)(+81 * 1E6);
	private int maxLat = (int)(-81 * 1E6);
	private int minLon  = (int)(+181 * 1E6);
	private int maxLon  = (int)(-181 * 1E6);
	private int lat;
	private int lon;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);

		lat = (int)(getIntent().getDoubleExtra("lat",0)* 1E6);
		lon = (int)(getIntent().getDoubleExtra("lon",0)* 1E6);

		mapView = (MapView) findViewById(R.id.mapView);

		mapController = mapView.getController();
		
		setupMap();

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}



	private void setupMap(){

		mapView.setBuiltInZoomControls(true); 
		mapView.displayZoomControls(true);

		if (lat != 0 && lon !=0)  {

			minLat = (minLat > lat) ? lat : minLat;
			maxLat = (maxLat < lat) ? lat : maxLat;                
			minLon = (minLon > lon) ? lon : minLon;
			maxLon = (maxLon < lon) ? lon : maxLon;
			
			destination = new GeoPoint(lat,lon);
			
			mapController.zoomToSpan((maxLat - minLat),(maxLon - minLon));
			mapController.animateTo(new GeoPoint((maxLat + minLat)/2,(maxLon + minLon)/2 ));

			MapOverlay mapOverlay = new MapOverlay();
			List<Overlay> listOfOverlays = mapView.getOverlays();
			listOfOverlays.clear();
			listOfOverlays.add(mapOverlay);   

			mapView.invalidate();

		}



	}


	class MapOverlay extends com.google.android.maps.Overlay
	{
		@Override
		public boolean draw(Canvas canvas, MapView mapView, 
				boolean shadow, long when) 
		{
			super.draw(canvas, mapView, shadow);                   

			//---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(destination, screenPts);

			//---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(
					getResources(), R.drawable.pushpin); 
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
			return true;
		}
	} 

}
